package com.wuzf.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;

public class Realm1 implements Realm{

	public String getName() {
		return "myrealm1";
	}

	public boolean support(AuthenticationToken token) {
		//仅支持UsernamePasswordToken类型的token
		return token instanceof UsernamePasswordToken;
	}

	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//得到用户名
		String username = (String)token.getPrincipal();
		//得到密码
		String password = new String((char[])token.getCredentials());
		System.out.println(username + ":" + password);
		if (!"zhang".equals(username)) {
			throw new UnknownAccountException();
		}
		if (!"123".equals(password)) {
			throw new IncorrectCredentialsException();
		}
		//如果身份验证成功返回一个AuthenticationInfo实现
		return new SimpleAuthenticationInfo(username, password, getName());
	}
	
}
